﻿using System.Collections;
using System.Collections.Generic;

namespace h94
{
    public class Solution {
        public int Calculate(string s) {
            s = s.Replace(" ", "");
            char[] s1 = s.ToCharArray();
            Stack ts = new Stack();
            int len = s1.Length;
            int ans = 0;
            int num = 0;
            char opt = '+';  //初始化opt为'+',是为了把第一个数放入堆栈
            char ch;
            for (int i = 0; i < len; i++)
            {
                while (char.IsDigit(s1[i]))
                {
                    num = num * 10 + s1[i] - '0';
                    i++;
                    if (i >= len)
                    {
                        i--;
                        break;
                    }
                }
                ch = s1[i];
                switch (opt)
                {
                    case '+':
                        ts.Push(num);
                        break;
                    case '-':
                        ts.Push(-num);
                        break;
                    case '*':
                        ts.Push((int)ts.Pop() * num);
                        break;
                    case '/':
                        ts.Push((int)ts.Pop() / num);
                        break;
                    default:
                        break;
                }
                num = 0;
                opt = ch;
            }
            while (ts.Count > 0)
            {
                ans += (int)ts.Pop();
            }
            return ans;        
        }
    }

}